﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using System.ComponentModel;
namespace WPFExcelReport
{
    public partial class TSTP : ViewBase
    {
        public VGPDAT TMV
        {
            get
            {
                return (VGPDAT)MV;
            }
        }
        #region ==PARTIE===========
        public static readonly DependencyProperty PARTIEProperty = DependencyProperty.Register("PARTIE",
            typeof(string), typeof(TSTP),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnPARTIEChanged));
        public string PARTIE
        {
            get
            {
                return (string)GetValue(PARTIEProperty);
            }
            set
            {
                SetValue(PARTIEProperty, value);
                Locate();
            }
        }
        private static void OnPARTIEChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TSTP;
            if (src != null)
            {
                src.PARTIE = (string)e.NewValue;
            }
        }
        #endregion

        #region ==GSART===========
        public static readonly DependencyProperty GSARTProperty = DependencyProperty.Register("GSART",
            typeof(string), typeof(TSTP),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnGSARTChanged));
        public string GSART
        {
            get
            {
                return (string)GetValue(GSARTProperty);
            }
            set
            {
                SetValue(GSARTProperty, value);
                Load();
            }
        }
        private static void OnGSARTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TSTP;
            if (src != null)
            {
                src.GSART = (string)e.NewValue;
            }
        }
        #endregion

        #region ==KEY===========
        public static readonly DependencyProperty KEYProperty = DependencyProperty.Register("KEY",
            typeof(string), typeof(TSTP),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnKEYChanged));
        public string KEY
        {
            get
            {
                return (string)GetValue(KEYProperty);
            }
            set
            {
                SetValue(KEYProperty, value);
            }
        }
        private static void OnKEYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TSTP;
            if (src != null)
            {
                src.KEY = (string)e.NewValue;
            }
        }
        #endregion 

        #region ==dwGP===========
        public static readonly DependencyProperty dwGPProperty = DependencyProperty.Register("dwGP",
            typeof(DataRow), typeof(TSTP),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OdwGPChanged));
        public DataRow dwGP
        {
            get
            {
                return (DataRow)GetValue(dwGPProperty);
            }
            set
            {
                SetValue(dwGPProperty, value); 
            }
        }
        private static void OdwGPChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TSTP;
            if (src != null)
            {
                DataRow row = (DataRow)e.NewValue;
                src.dwGP = row;
            }
        }
        #endregion
        public TSTP()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return; 
            MV = new VGPDAT();
            this.DataContext = MV; 
            InitializeComponent();
         }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            dwGP = dwMaster;
            if (dwGP != null)
            {
                KEY = dwGP["GPZWPART"].ToString();
            }
            else
            {
                KEY = "";
            }
        }
   
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            base.Window_Loaded(sender, e);
          
        }
        public void Load()
        {
            if (GSART.Length < 23)
                return;
            dwData = TBMain.NewRow();
            dwData["GPMANDANT"] = app.Mandant;
            dwData["GPGART"] = GSART.Substring(0, 20);
            dwData["GPGVAR"] = GSART.Substring(20, 3);
            SResult = TMV.QueryGPDAT(dwData, -4);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                row["KEY"] = Helpers.Helper.GetGB2312Value(row["KEY"].ToString());
            }
            MergeData(SResult,false); 
        }
        public void Locate()
        {
            if (TBMain == null)
                return; 
            int idx = -1; 
            var query = from DataRow row in TBMain.AsEnumerable()
                        where row["GPZWPART"].ToString().Trim() == PARTIE.Trim()
                        select row;
            foreach (DataRow row in query)
            {
                 idx = TBMain.Rows.IndexOf(row);              
                break;
            }
            vwMaster.View.MoveCurrentTo(idx);
        }
        public void Limit()
        {
            if (MV.TBTemp == null)
                MV.TBTemp = TBMain.Copy();
            if (GSART.Length < 22)
            {
                TBMain.Clear();
                if (MV.TBTemp != null)
                    TBMain.Merge(MV.TBTemp);
                return;
            }
            filter = "GPGART='"+GSART.Substring(0,20)+"' AND GPGVAR='"+GSART.Substring(20,2)+"'";
            rows = MV.TBTemp.Select();
            if (rows.Length > 0)
            {
                TBMain.Clear();
                TBMain.Merge(rows.CopyToDataTable());
            }
        }
        public override void ManageCommand(object sender, RoutedEventArgs e)
        {
            if (IsManage)
            {
                Load();
            }
            else
            {
                VICC_WFEVENTS.SVWF.GP02(this.RUNID);
            }
            base.ManageCommand(sender, e);
        }
    }
}
